package com.ler.yrmall.member.controller;

import com.ler.yrmall.basic.Form;
import com.ler.yrmall.basic.Result;
import com.ler.yrmall.dto.LoginUser;
import com.ler.yrmall.member.entity.MemberEntity;
import com.ler.yrmall.member.service.MemberService;
import com.ler.yrmall.utils.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;


/**
 * @author Leron
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Resource
    private MemberService memberService;


    /**
     * 列表
     */
    @RequestMapping("/find")
    public Result<?> find(Form request) {
        return Result.ok(memberService.find(request));
    }

    @RequestMapping("/list")
    public Result<?> list() {
        return Result.ok(memberService.list());
    }

    /**
     * 信息
     */
    @RequestMapping("/getById/{id}")
    public Result<?> getById(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);
        return Result.ok(member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result<?> save(@RequestBody MemberEntity member) {
        memberService.save(member);
        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result<?> update(@RequestBody MemberEntity member) {
        memberService.updateById(member);
        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result<?> delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

//    @PostMapping("register")
//    public Result<?> register(@RequestBody UserRegisterVO userRegisterVO) {
//        try {
//            memberService.register(userRegisterVO);
//        } catch (PhoneExistException e) {
//            return Result.error("手机号存在");
//        } catch (UsernameExistException e) {
//            return Result.error("用户名存在");
//        }
//        return Result.ok();
//    }


    // ----- 移动端
    @GetMapping("/info")
    public Result<?> userInfo() {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        return Result.ok(memberService.getById(currentUser.getId()));
    }

    @GetMapping("/checkin")
    public Result<?> checkin() {
        return Result.ok(memberService.checkin());
    }

}
